<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>文章管理</el-breadcrumb-item>
      <el-breadcrumb-item>评论列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索区域 -->
      <el-row :gutter="20">
        <el-col :span="8"
          ><el-input
            placeholder="请输入内容"
            v-model="selectMsg.msg"
            clearable
            @clear="selectCommentList()"
          >
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="Select"
            ></el-button> </el-input
        ></el-col>
      </el-row>
      <!-- 表格区域 -->
      <el-table :data="commentlist" stripe border>
        <el-table-column prop="id" label="id" width="50px"> </el-table-column>
        <el-table-column
          prop="commentContent"
          label="评论内容"
        ></el-table-column>
        <el-table-column prop="usersId" label="评论者id"> </el-table-column>
        <el-table-column prop="articlesId" label="文章id"> </el-table-column>
        <el-table-column prop="createdTime" label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.createdTime | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column label="操作" width="130px">
          <template slot-scope="scope">
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="Delect(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="this.pager.currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="this.pager.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.pager.total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  mounted () {
    this.GetAllComment(this.pager)
  },
  data () {
    return {
      // 要操作的行
      currentIndex: 0,
      // 评论列表
      commentlist: [
      ],
      // 分页用到的数据
      pager: {
        currentPage: 1,
        pageSize: 5,
        total: 0
      },
      selectMsg: {
        msg: ''
      }
    }
  },
  methods: {
    // 获取所有评论
    async GetAllComment (params) {
      const { data: res } = await this.$http.get('comment', { params: params })
      if (res.data) {
        this.commentlist = res.data
        this.pager.total = res.count
      }
    },
    // 删除
    async Delect (id) {
      await this.$http.delete(`comment/deletecomment/${id}`)
      this.GetAllComment(this.pager)
    },
    // 查询你按钮逻辑
    Select () {
      if (this.selectMsg.msg) {
        this.SelectComment(this.selectMsg)
      } else {
        this.GetAllComment(this.pager)
      }
    },
    // 评论内容查询
    async SelectComment (params) {
      params.currentPage = this.pager.currentPage
      params.pageSize = this.pager.pageSize
      params.total = this.pager.total
      const { data: res } = await this.$http.get('comment/select', { params: params })
      if (res.data) {
        this.commentlist = res.data
        this.pager.total = res.count
      }
    },
    handleSizeChange (newSize) {
      // pagesize改变触发
      this.pager.pageSize = newSize
      this.GetAllComment(this.pager)
    },
    handleCurrentChange (newPage) {
      // 页码改变触发
      this.pager.currentPage = newPage
      this.GetAllComment(this.pager)
    },
    // 清空搜索框重新获取数据
    selectCommentList () {
      this.GetAllComment(this.pager)
    }
  }
}
</script>

<style scoped>
</style>
